package com.zhang.crowd.mvc.handler;

import com.zhang.crowd.entity.Auth;
import com.zhang.crowd.entity.Role;
import com.zhang.crowd.service.api.AdminService;
import com.zhang.crowd.service.api.AuthService;
import com.zhang.crowd.service.api.RoleService;
import com.zhang.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {
    @Autowired
    AdminService adminService;

    @Autowired
    RoleService roleService;

    @Autowired
    AuthService authService;

    @PreAuthorize("hasRole('部长')")
    @ResponseBody
    @RequestMapping("assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelationship(@RequestBody Map<String,List<Integer>> map){
        authService.saveRoleAuthRelationship(map);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId){
        //查询指定角色的权限
        List <Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    @ResponseBody
    @RequestMapping("assign/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth(){

        List<Auth> list = authService.getAll();
        return ResultEntity.successWithData(list);
    }


    @RequestMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,

            //我们允许用户在页面上取消所有已分配角色再提交表单，所以可以不提供roleList请求参数
            //设置required=false表示这个请求参数不是必须
            @RequestParam(value = "roleIdList" , required = false) List<Integer> roleIdList
    ){

        adminService.saveAdminRoleRelationship(adminId,roleIdList);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }



    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignPage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
    ){
        //查询已分配的角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        //查询未分配的角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);

        //存入模型 (本质是其实是request.setAttribute("attrName","attrValue")
        modelMap.addAttribute("assignedRoleList",assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList",unAssignedRoleList);

        return "assign-role";
    }


}
